﻿using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using com.magickms.api.User32;
using com.magickms.util;

namespace com.magickms.sln
{
    public partial class FindCmp : Form
    {
        private IntPtr _findWindow;

        public FindCmp()
        {
            InitializeComponent();
            this.Region = Util.ReShape(0, 0, Width, Height, 2, true);
        }

        public IntPtr UserControl
        {
            get
            {
                return _findWindow;
            }
            set
            {
                _findWindow = value;
                DisplayInfo(_findWindow);
            }
        }

        #region 鼠标事件处理
        private IntPtr _lastWindow = IntPtr.Zero;
        private void PbApp_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Bitmap bmp = (Bitmap)Bitmap.FromFile(@"ico\_cur.png");
                Cursor = new Cursor(bmp.GetHicon());
                PbApp.Image = global::com.magickms.Properties.Resources._run;
            }
        }

        private void PbApp_MouseMove(object sender, MouseEventArgs e)
        {
            if (Cursor != Cursors.Default)
            {
                IntPtr FoundWindow = User32.ChildWindowFromPoint(Cursor.Position);

                // not this application
                Control control = Control.FromHandle(FoundWindow);
                if (control == null)
                {
                    if (FoundWindow != _lastWindow)
                    {
                        // clear old window
                        User32.ShowInvertRectTracker(_lastWindow);
                        // set new window
                        _lastWindow = FoundWindow;
                        // paint new window
                        User32.ShowInvertRectTracker(_lastWindow);
                    }
                    DisplayInfo(_lastWindow);
                }
                // show global mouse cursor
                //Point p = Cursor.Position;
                //LbPos.Text = "位置：[" + p.X + "," + p.Y + "]";
            }
        }

        private void PbApp_MouseUp(object sender, MouseEventArgs e)
        {
            if (Cursor != Cursors.Default)
            {
                // reset all done things from mouse_down and mouse_move ...
                User32.ShowInvertRectTracker(_lastWindow);
                _findWindow = _lastWindow;
                _lastWindow = IntPtr.Zero;

                Cursor = Cursors.Default;
                PbApp.Image = global::com.magickms.Properties.Resources._def;
            }
        }

        /// <summary>
        /// Show informations about the given window
        /// </summary>
        /// <param name="window"></param>
        private void DisplayInfo(IntPtr window)
        {
            if (window == IntPtr.Zero)
            {
                // reset all edit box
                LbCmpLoc.Text = "";
            }
            else
            {
                // Handler
                LbTitle.Text = "组件句柄：" + window;

                // Point
                Point point = Cursor.Position;
                User32.ScreenToClient(window, ref point);
                TbCmpLoc.Text = point.ToString();

                // Rect
                Rectangle WindowRect = User32.GetWindowRect(window);
                TbCmpDim.Text = WindowRect.ToString();

                // Caption
                TbCmpTitle.Text = User32.GetWindowText(window);

                // Class
                StringBuilder ClassName = new StringBuilder(256);
                User32.GetClassName(window, ClassName, ClassName.Capacity);
                TbCmpClass.Text = ClassName.ToString();
            }
        }
        #endregion

        #region 窗口按钮事件
        private void PbMore_Click(object sender, EventArgs e)
        {
            WndList wl = new WndList(this);
            wl.Show(this);
            wl.UserControl = _findWindow;
        }

        private void PbOK_Click(object sender, EventArgs e)
        {
            Close();
            DialogResult = DialogResult.OK;
        }

        private void PbCancel_Click(object sender, EventArgs e)
        {
            Close();
            DialogResult = DialogResult.Cancel;
        }
        #endregion

        #region 窗口加载事件
        private void FindCmp_Load(object sender, EventArgs e)
        {
            bgTl = Image.FromFile("png/bgtl.png");
            bgTm = Image.FromFile("png/bgtm.png");
            bgTr = Image.FromFile("png/bgtr.png");

            bgMl = Image.FromFile("png/bgml.png");
            bgMm = Image.FromFile("png/bgmm.png");
            bgMr = Image.FromFile("png/bgmr.png");

            bgBl = Image.FromFile("png/bgbl.png");
            bgBm = Image.FromFile("png/bgbm.png");
            bgBr = Image.FromFile("png/bgbr.png");

            ReDraw();
        }

        private void FindCmp_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(bg, 0, 0);
        }

        private void FindCmp_Resize(object sender, EventArgs e)
        {
            //ReDraw();
        }
        #endregion

        #region 窗口特效

        private Image bg;
        private Image bgTl;
        private Image bgTm;
        private Image bgTr;

        private Image bgMl;
        private Image bgMm;
        private Image bgMr;

        private Image bgBl;
        private Image bgBm;
        private Image bgBr;

        private void ReDraw()
        {
            bg = new Bitmap(Width, Height);
            Graphics g = Graphics.FromImage(bg);

            int x = 0;
            int y = 0;
            int w = Width - bgTr.Width;
            DoDraw(g, bgTl, bgTm, bgTr, x, y, w);

            x = 0;
            y += bgTl.Height;
            w = Width - bgMr.Width;
            DoDraw(g, bgMl, bgMm, bgMr, x, y, w);

            int h = Height - bgBl.Height;
            while (y < h)
            {
                x = 0;
                y += bgMl.Height;
                DoDraw(g, bgMl, bgMm, bgMr, x, y, w);
            }

            x = 0;
            y = h;
            w = Width - bgBr.Width;
            DoDraw(g, bgBl, bgBm, bgBr, x, y, w);
        }

        private void DoDraw(Graphics g, Image imgL, Image imgM, Image imgR, int x, int y, int w)
        {
            g.DrawImage(imgL, x, y);

            x += imgL.Width;
            g.DrawImage(imgM, x, y);

            while (x < w)
            {
                x += imgM.Width;
                g.DrawImage(imgM, x, y);
            }

            g.DrawImage(imgR, w, y);
        }
        #endregion

        #region 窗口移动
        private Point _mouseOffset;
        private bool _isMouseDown;
        private void PlTitle_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }
            _mouseOffset = new Point(-e.X, -e.Y);
            _isMouseDown = true;
        }

        private void PlTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!_isMouseDown)
            {
                return;
            }

            var mousePos = MousePosition;
            mousePos.Offset(_mouseOffset);
            Location = mousePos;
        }

        private void PlTitle_MouseUp(object sender, MouseEventArgs e)
        {
            //修改鼠标状态isMouseDown的值
            //确保只有鼠标左键按下并移动时，才移动窗体
            if (e.Button == MouseButtons.Left)
            {
                _isMouseDown = false;
            }
        }
        #endregion
    }
}
